package com.niannian.util;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;

public class AudioPlayer implements OnBufferingUpdateListener, OnCompletionListener, MediaPlayer.OnPreparedListener {
	public MediaPlayer mediaPlayer;
	private SeekBar skbProgress;
	private TextView tv_player_time;

	public AudioPlayer() {
		try {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setOnBufferingUpdateListener(this);
			mediaPlayer.setOnPreparedListener(this);
		} catch (Exception e) {
			Log.e("mediaPlayer", "error", e);
		}
	}

	public void ShowView(SeekBar skbProgress, TextView tv_player_time) {
		this.skbProgress = skbProgress;

		this.tv_player_time = tv_player_time;

	}

	public void play() {
		mediaPlayer.start();
		if (skbProgress != null) {
			skbProgress.setMax(mediaPlayer.getDuration());
			starTimer();
		}
	}

	public void playUrl(String videoUrl) {
		try {
			mediaPlayer.reset();
			mediaPlayer.setDataSource(videoUrl);
			mediaPlayer.prepare();// prepare之后自动播放
			// mediaPlayer.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void pause() {
		if (mediaPlayer != null) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.pause();
			}
		}
		stopTimer();

	}

	public void stop() {
		try {

			if (mediaPlayer != null) {
				mediaPlayer.stop();
				mediaPlayer.release();
				mediaPlayer = null;
			}
			stopTimer();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	/**  
	 * 通过onPrepared播放  
	 */
	public void onPrepared(MediaPlayer arg0) {
		arg0.start();
		Log.e("mediaPlayer", "onPrepared");
	}

	@Override
	public void onCompletion(MediaPlayer arg0) {
		stop();
	}

	@Override
	public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
//		if (!Common.empty(skbProgress)) {
//			skbProgress.setSecondaryProgress(bufferingProgress);
//			int currentProgress = skbProgress.getMax() * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
//			Log.e(currentProgress + "% play", bufferingProgress + "% buffer");
//		}
	}

	Handler handleProgress = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				if (mediaPlayer != null) {
					int position = mediaPlayer.getCurrentPosition();
					int duration = mediaPlayer.getDuration();
					if (duration > 0) {
						if (skbProgress != null) {
							long pos = skbProgress.getMax() * position / duration;
							skbProgress.setProgress(position);
						}

						if (tv_player_time != null) {
							tv_player_time.setText(Common.showAudLen(position / 1000) + "/" + Common.showAudLen(duration / 1000));
						}
					}

					if (position == duration) {
						stopTimer();
					}
				}
				break;
			default:
				break;
			}

			super.handleMessage(msg);
		};
	};

	/*******************************************************
	 * 通过定时器和Handler来更新进度条
	 ******************************************************/

	Timer timer;
	TimerTask task;

	private void starTimer() {
		if (timer == null) {
			timer = new Timer(true);
		}
		if (task == null) {
			task = new TimerTask() {
				public void run() {
					try {
						if (mediaPlayer == null)
							return;

						if (skbProgress == null && mediaPlayer.isPlaying()) {
							handleProgress.sendEmptyMessage(0);
						} else if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
							handleProgress.sendEmptyMessage(0);
						}
					} catch (Exception e) {

					}
				}
			};
		}

		try {
			if (task != null && timer != null) {
				timer.schedule(task, 0, 10); 
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void stopTimer() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (task != null) {
			task.cancel();
			task = null;
		}
	}
}